package com.example.doer.reading.AppActivities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbStringHttpResponseListener;
import com.ab.image.AbImageLoader;
import com.ab.view.titlebar.AbTitleBar;
import com.example.doer.reading.AppDataModel.Book;
import com.example.doer.reading.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class OtherBooks extends AbActivity {

    AbImageLoader abImageLoader;
    private List<Book> list;
    private String imageURL="http://192.168.191.1/Reading/ImageDoServlet?Action=download&Category=BookCover&ImageName=";
    private AbTitleBar abTitleBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.activity_other_books);
        abTitleBar=getTitleBar();
        abImageLoader= AbImageLoader.getInstance(this);
        final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linear_other);
//        linearLayout.addView(addOtherBook(R.drawable.image5,"","贼好看","王博林","贼好看666666666666666666666666666666666666","5000"));
        final String category=getIntent().getStringExtra("Category");
        final boolean ifIsCategoryList=getIntent().getBooleanExtra("ifIsCategoryList",true);
        abTitleBar.setTitleText(category);
        abTitleBar.setLogo(R.mipmap.return_button);
        abTitleBar.setTitleBarBackgroundColor(getResources().getColor(R.color.black));
        String url=null;
        if (ifIsCategoryList) {
            url="http://192.168.191.1/Reading/BookStoreServlet?Action=CategoryList&Category="+category;
        }
        else{
            url="http://192.168.191.1/Reading/BookStoreServlet?Action=Recommend&Category="+category;
        }


        AbHttpUtil abHttpUtil= AbHttpUtil.getInstance(this);
        abHttpUtil.get(url, new AbStringHttpResponseListener() {
            @Override
            public void onSuccess(int i, String s) {
                try {
                    JSONObject jsonObject=new JSONObject(s);
                    JSONArray jsonArray=jsonObject.getJSONArray("BookList");


                    for (int j=0;j<jsonArray.length();j++){
                        JSONObject object=jsonArray.getJSONObject(j);
                        Book book=new Book(object.getString("BookID"),
                                object.getString("BookName"),
                                object.getString("Category"),
                                object.getString("BookDate"),
                                object.getString("BookAuthor"),
                                object.getString("BookDescription"),
                                object.getInt("BookPrice"));
                        linearLayout.addView(addOtherBook(book));
                    }

//                    for (int j=0;j<jsonArray.length();j++){
//                        JSONObject action=jsonArray.getJSONObject(j);
//
//                        //加入参数
//                        //linearLayout.addView(addOtherBook());
//                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {

            }

            @Override
            public void onFinish() {

            }

            @Override
            public void onFailure(int i, String s, Throwable throwable) {

            }
        });
    }
    private LinearLayout addOtherBook(final Book book)
    {
        LayoutInflater inflater =(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout child = (LinearLayout) inflater.inflate(R.layout.item_other_book,null);
        ImageView imageView1 = (ImageView) child.findViewById(R.id.iv_otherbook);
        TextView tv_bookName = (TextView) child.findViewById(R.id.tv_otherbookName);
        TextView tv_author = (TextView)child.findViewById(R.id.tv_otherbookAuthor);
        TextView tv_discription = (TextView)child.findViewById(R.id.tv_discription);
        TextView tv_price = (TextView)child.findViewById(R.id.tv_otherbookprice);


        tv_bookName.setText(book.getBookName());
        tv_author.setText(book.getBookAuthor());
        tv_discription.setText(book.getBookDescription());
        tv_price.setText(String.valueOf(book.getBookPrice()));
        //imageView1.setImageResource(res);
        abImageLoader.display(imageView1,imageURL+book.getBookID(),300,300);

        child.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Intent intent=new Intent(OtherBooks.this, BookDetailActivity.class);
                intent.putExtra("book",book);
                startActivity(intent);
                return false;
            }
        });
        return child;
    }
}
